
class MyPlane{
    constructor(option){
        this.width = 258
        this.height = 364
        this.top = 240
        this.left = 240
        this.dom = createImg()
        this.dom.src = option.resource
        this.dom.style.animation = 'myPlaneAnimation 1.2s linear'
    }
    patch(){
        render(this.dom,this.top,this.left) 
    }
}


//创建飞机，渲染到界面，做动画
const myPlane = new MyPlane({resource:'./assets/plane.png'})

    //渲染style属性，注：初始化时做动画
myPlane.patch()

    //追加到界面
app.appendChild(myPlane.dom)

//定时监听commands数据，改变位置，重新渲染   执行函数
const move = ()=>{
    //核实上下键
    const command1 = commands.find((item)=>{
        return item.type === 'upAndDown'
    })
    if(command1){
        myPlane.top += command1.dir*10
        //边界判断
        if(myPlane.top <= 0){
            myPlane.top = 0
        }
        if(myPlane.top >= 1080-364){
            myPlane.top = 1080-364
        }
    }

    //核实左右键
    const command2 = commands.find((item)=>{
        return item.type === 'leftAndRight'
    })
    if(command2){
        myPlane.left += command2.dir*10
        //边界判断
        if(myPlane.left <= 0){
            myPlane.left = 0
        }
        if(myPlane.left >= 750-258){
            myPlane.left = 750-258
        }
    }

    //重新渲染
    render(myPlane.dom,myPlane.top,myPlane.left)
}

//定时监控，飞机移动
setInterval(move,20)

//定时监控，传递位置数据，用于创建子弹   执行函数
const updateMyBulletsArr = ()=>{
    if(status){
        if(interval === 20){
            const option = {
                top: myPlane.top,
                left: myPlane.left + 105,
                speed: 5,
                dir: -1,
                resource: './assets/bunny-self.png'
            }
            const bullet = new MyBullet(option)
            myBulletsArr.push(bullet)
            bullet.patch()
            app.appendChild(bullet.dom)
        }
        if(--interval < 0){
            interval = 20
        }
    }
}

setInterval(updateMyBulletsArr,20)




